﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Hyt.Web
{

    public class HttpHelper
    {
        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="context"></param>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        public static void WriteCookie(HttpContext context, string strName, string strValue)
        {
            var cookie = context.Request.Cookies[strName] ?? new HttpCookie(strName);
            cookie.Value = strValue;
            context.Response.AppendCookie(cookie);
        }


        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="context"></param>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        /// <param name="expires"></param>
        public static void WriteCookie(HttpContext context, string strName, string strValue, int? expires=null)
        {
            var cookie = context.Request.Cookies[strName] ?? new HttpCookie(strName);
            cookie.Value = strValue;
            if (expires != null)
            {
                cookie.Expires = DateTime.Now.AddMinutes(expires.Value);
            }
            context.Response.AppendCookie(cookie);

        }

        /// <summary>
        /// 读cookie值
        /// </summary>
        /// <param name="context"></param>
        /// <param name="strName">名称</param>
        /// <returns>cookie值</returns>
        public static string GetCookie(HttpContext context, string strName)
        {
            return context.Request.Cookies[strName] != null ? context.Request.Cookies[strName].Value : "";
        }


        /// <summary>
        /// 读Session值
        /// </summary>
        /// <param name="context"></param>
        /// <param name="strName">名称</param>
        /// <returns>Session值</returns>
        public static string GetSession(HttpContext context, string strName)
        {
            if (context.Session == null || context.Session[strName] == null) return "";
            string value = context.Session[strName].ToString();
            return value;
        }

        /// <summary>
        /// 获得当前页面客户端的IP
        /// </summary>
        /// <returns>当前页面客户端的IP</returns>
        public static string GetIP(HttpContext context)
        {
            string result = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(result))
            {
                result = context.Request.ServerVariables["REMOTE_ADDR"];
            }

            if (string.IsNullOrEmpty(result))
            {
                result = context.Request.UserHostAddress;
            }

            if (string.IsNullOrEmpty(result) || !System.Text.RegularExpressions.Regex.IsMatch(result, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"))
            {
                return "0.0.0.0";
            }

            return result;

        }
    }
}
